from django.contrib import admin
from .models import *
from import_export import resources
from import_export.admin import ImportExportModelAdmin, ImportExportActionModelAdmin
import math 
from django.db.models import Count 

from django.db.models import Avg, Max, Min
from django.db.models import Sum
 
'''
class ProxyResource(resources.ModelResource):
    class Meta:
        model = Resource

# Register your models here.
@admin.register(Resource)

#class RecordAdmin(admin.ModelAdmin):
#class RecordAdmin(ImportExportModelAdmin):

class ResourceAdmin(ImportExportActionModelAdmin):
	resources_class =ProxyResource
'''

class ProxyResource(resources.ModelResource):
    class Meta:
        model = ViewsByDayModel

# Register your models here.
@admin.register(ViewsByDayModel)
class ViewsByDayAdmin(ImportExportModelAdmin):
    list_filter = (
        'date',
    )
    change_list_template = 'admin/admin_test.html'
 
    def changelist_view(self, request, extra_context=None):
        response = super().changelist_view(
            request,
            extra_context=extra_context
        )
 
        try:
            qs = response.context_data['cl'].queryset
        except (AttributeError, KeyError):
            return response
 
        metrics = {
            'days': Count('date'), # date是model类的字段
            'cpu_count': Max('cpu_count'), # views_count是model累的字段
            'gpu_count': Max('gpu_count'), # ip_count是model类的字段
            'mem_count': Max('mem_count'),
 
        }
        response.context_data['date'] = list(
            qs
                .values('date')
                .annotate(**metrics)
        )
 
        return response

